using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Microsoft.ApplicationBlocks.Updater.Tools
{
    public class ManifestSourceFilesPath
    {
        public static string GetFilename()
        {
            return Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),
                "ManifestSourceFiles.xml");
        }

        public static FileSources ReadFileSources()
        {
            return ReadFileSources(GetFilename());
        }

        public static FileSources ReadFileSources(string filename)
        {
            FileSources filesources = null;

            if (File.Exists(GetFilename()))
            {
                XmlSerializer ser = new XmlSerializer(typeof(FileSources));
                using (StreamReader sr = new StreamReader(filename))
                {
                    filesources = (FileSources)ser.Deserialize(sr);
                }
            }
            return filesources;
        }

        public static void WriteFileSources(FileSources filesources)
        {
            WriteFileSources(GetFilename(), filesources);
        }

        public static void WriteFileSources(string filename, FileSources filesource)
        {
            XmlSerializer ser = new XmlSerializer(typeof(FileSources));
            using (StreamWriter sr = new StreamWriter(filename))
            {
                ser.Serialize(sr, filesource);
            }
        }

        public static void AddFileSource(FileSources filesources, FileSource filesource)
        {
            List<FileSource> filesourceList = new List<FileSource>(filesources.FileSource);

            filesourceList.Add(filesource);
            filesources.FileSource = filesourceList.ToArray();
        }

        public static void AddFileSource(FileSource filesource)
        {
            FileSources filesources = ReadFileSources();
            if (filesources == null)
            {
                filesources = new FileSources();
                filesources.FileSource = new FileSource[0];
            }
            List<FileSource> filesourceList = new List<FileSource>(filesources.FileSource);
            bool found = false;

            filesourceList.ForEach(delegate(FileSource fs)
            {
                if (fs.ManifestId.Equals(filesource.ManifestId, StringComparison.OrdinalIgnoreCase))
                {
                    fs.SourcePath = filesource.SourcePath;
                    found = true;
                }
            });
            if (!found)
            {
                filesourceList.Add(filesource);
                filesources.FileSource = filesourceList.ToArray();
            }

            XmlSerializer ser = new XmlSerializer(typeof(FileSources));
            using (StreamWriter sr = new StreamWriter(GetFilename()))
            {
                ser.Serialize(sr, filesources);
            }
        }
    }
}
